package com.coder520.mamabike.utils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http请求工具
 */
public class HttpUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
    /**
     * post请求
     *
     * @param url 功能和操作
     * @return
     * @throws IOException
     */
    public static String post(String url, Map<String, String> params)  {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse httpResponse = null;

        try {
            HttpPost httpPost = new HttpPost(url);

            if (params != null) {
                List<NameValuePair> nameValuePairList = Lists.newArrayList();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue());
                    nameValuePairList.add(nameValuePair);
                }
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, "utf-8"));
            }
            httpResponse = client.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();

            if(httpEntity!=null){
                responseText = EntityUtils.toString(httpEntity);
            }
        } catch (Exception e) {
            logger.error("http请求失败",e);

        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return responseText;
    }

    public static String get(String url, Map<String, String> params)  {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse httpResponse = null;

        try {
            String getUrl = url+"?";

            if (params != null) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    getUrl+=entry.getKey()+"=" + URLEncoder.encode(entry.getValue(),"utf-8")+"&";
                }
            }
            HttpGet httpGet = new HttpGet(getUrl);
            httpResponse = client.execute(httpGet);

            HttpEntity httpEntity = httpResponse.getEntity();

            if(httpEntity!=null){
                responseText = EntityUtils.toString(httpEntity);
            }
        } catch (Exception e) {
            logger.error("http请求失败",e);

        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return responseText;
    }
}